package com.woniu.service.impl;

import com.woniu.dao.AdminMapper;
import com.woniu.pojo.Admin;
import com.woniu.service.AdminService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> findAdminByPage(Integer currentPage, Integer pageSize) {

        return adminMapper.selectByPage(currentPage, pageSize);
    }

    @Override
    public void saveAdmin(Admin admin) {
        //密码加密
        String salt = UUID.randomUUID().toString();
        System.out.println(salt);
        admin.setSalt(salt);
        Md5Hash hash = new Md5Hash(admin.getPassword(),salt,1024);
        admin.setPassword(hash.toString());//加密后的密码
        adminMapper.insertSelective(admin);
    }

    @Override
    public void batchDel(List<Integer> list) {
        adminMapper.batchDel(list);
    }

    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin checkName(Admin admin) {
        return adminMapper.selectByUserName(admin.getUsername());
    }

    @Override
    public void updateAdmin(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }
}
